//
//  ScaleRulerView.m
//  SPMapKit
//
//  Created by zhangbo on 2017/9/12.
//  Copyright © 2017年 zhangbo. All rights reserved.
//

#import "ScaleRulerView.h"

@implementation ScaleRulerView

-(instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.scaleStyle = DARK_STYLE;
    }
    
    return self;
}

-(void) setScaleStyle:(SPScaleStyle)scaleStyle
{
    switch (scaleStyle) {
        case DARK_STYLE:
            _mColor = [UIColor blackColor];
            _sColor = [UIColor whiteColor];
            _fontColor = [UIColor blackColor];
            break;
        case LIGHT_STYLE:
            _mColor = [UIColor whiteColor];
            _sColor = [UIColor grayColor];
            _fontColor = [UIColor whiteColor];
            break;
            
        default:
            break;
    }
    
    [self setNeedsDisplay];
}

-(void) show:(BOOL) show animated:(BOOL) animated
{
    if (animated) {
        [UIView animateWithDuration:show ? 0.5:1.2 animations:^{
            self.alpha = show ? 1:0;
        }];
    }else{
        self.alpha = show ? 1:0;
    }
}

-(void) drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    int segCount = 2;
    
    int level = MAX(0, log2f(_zoomScale));
    double groundResolution = _groundResolution * pow(2, level) / _zoomScale;
    int unit = [self getUnit:level];
    if (groundResolution < _groundResolution / 1.5) {
        segCount = 2;
    }else{
        segCount = 3;
    }
    
    CGFloat curUnit = unit / groundResolution;
    
//    NSLog(@"gr:%f sgr:%f unit:%i curUnit:%f", _groundResolution, groundResolution, unit, curUnit);
    
    for (int i=0; i<segCount; i++) {
        CGRect rect = CGRectMake(i*curUnit, 10, curUnit, 3);
        if (i % 2 == 0) {
            CGContextSetFillColorWithColor(context, _mColor.CGColor);
            CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
            CGContextSetLineWidth(context, 0.2);
            CGContextAddRect(context,rect);
            CGContextDrawPath(context, kCGPathFillStroke);
        }else{
            CGContextSetFillColorWithColor(context, _sColor.CGColor);
            CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
            CGContextSetLineWidth(context, 0.2);
            CGContextAddRect(context,rect);
            CGContextDrawPath(context, kCGPathFillStroke);
        }
        
        NSString *txt = @"";
        if (i == segCount - 1) {
            if (unit * 2 < 1000) {
                txt = [NSString stringWithFormat:@"%d米", (i+1) * unit];
            }else if(unit * 2 < 10000){
                txt = [NSString stringWithFormat:@"%.1f公里", (i+1) * unit / 1000.f];
            }else{
                txt = [NSString stringWithFormat:@"%.0f公里", (i+1) * unit / 1000.f];
            }
        }else{
            if (unit * 2 < 1000) {
                txt = [NSString stringWithFormat:@"%d", (i+1) * unit];
            }else if(unit * 2 < 10000){
                txt = [NSString stringWithFormat:@"%.1f", (i+1) * unit / 1000.f];
            }else{
                txt = [NSString stringWithFormat:@"%.0f", (i+1) * unit / 1000.f];
            }
        }
        
        UIFont *font = [UIFont boldSystemFontOfSize:8.0];
        NSDictionary *attrDic = @{NSFontAttributeName:font, NSForegroundColorAttributeName:_fontColor};
        CGSize size = [txt boundingRectWithSize:CGSizeMake(999, 10) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDic context:nil].size;
        CGRect txtrect = CGRectMake((i+1)*curUnit - size.width / 2, 0, size.width, 10);
        [txt drawInRect:txtrect withAttributes:attrDic];
    }
}

-(int) getUnit:(int) level
{
    if (level > 19) {
        return 5;
    }
    
    return pow(2, MAX(0, 19 - level)) * 10;
}

@end
